﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using ActivityTrackingLog.Web.Helpers;
using ActivityTrackingLog.Utils;

namespace ActivityTrackingLog.Web.Utils
{
    internal class QueryParams
    {
        private HttpContext _context = null;
        public HtmlHelper.EntityEnum EntityType { get; set; }
        public HtmlHelper.ViewTypeEnum ViewType { get; set; }
        public HtmlHelper.ReportTypeEnum ReportType { get; set; }
        public bool EmbedStyles { get; set; }
        public string UserId { get; set; }
        public int PageIndex { get; set; }
        public int PageSize { get; set; }
        public ColumnData[] Columns { get; set; }

        public QueryParams(HttpContext context)
        {
            _context = context;

            EntityType = ReadQueryParamAsEnum<HtmlHelper.EntityEnum>("entity", HtmlHelper.EntityEnum.ActivityItem);
            ViewType = ReadQueryParamAsEnum<HtmlHelper.ViewTypeEnum>("view", HtmlHelper.ViewTypeEnum.Page);
            ReportType = ReadQueryParamAsEnum<HtmlHelper.ReportTypeEnum>("report", HtmlHelper.ReportTypeEnum.List);
            EmbedStyles = ReadQueryParam<bool>("styles", true);
            UserId = ReadQueryParam<string>("user", string.Empty);
            PageIndex = ReadQueryParam<int>("page_index", 0);
            PageSize = ReadQueryParam<int>("page_size", 20);
            Columns = GetColumns();
        }

        private ColumnData[] GetColumns()
        {
            var columns = ParseColumns(ReadQueryParam<string>("cols", string.Empty));
            var ascColumns = ParseColumns(ReadQueryParam<string>("asc_cols", string.Empty).ToLower());
            var descColumns = ParseColumns(ReadQueryParam<string>("desc_cols", string.Empty).ToLower());

            return columns.Select(c => 
                new ColumnData(
                    c,
                    null,
                    ascColumns.Contains(c.ToLower())? Enums.SortDirectionEnum.Ascending:
                    descColumns.Contains(c.ToLower())? Enums.SortDirectionEnum.Descending: Enums.SortDirectionEnum.None,
                    true)
            ).ToArray();
        }

        private string[] ParseColumns(string colsStr)
        {
            if (!string.IsNullOrWhiteSpace(colsStr))
                return colsStr.Split(new[] { ',' }).Select(c => c.Trim()).ToArray();
            return new string[] { };
        }

        #region helpers

        private T ReadQueryParamAsEnum<T>(string paramName, T defaultValue) where T : struct, IConvertible
        {
            if (!typeof(T).IsEnum)
            {
                throw new ArgumentException("T must be an enumerated type");
            }

            T enumValue;
            if (Enum.TryParse<T>(_context.Request[paramName], true, out enumValue))
                return enumValue;

            return defaultValue;
        }

        protected T ReadQueryParam<T>(string paramName, T defaultVal = default(T))
        {
            if (null != _context.Request[paramName])
                return _context.Request[paramName].ToString().Convert<T>(defaultVal);

            return defaultVal;
        }

        #endregion
    }
}
